<?php

namespace app\superadmin\controller;
use app\common\controller\SuperadminBase;
use app\superadmin\model\City as Model;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;



class City extends SuperadminBase

{



    public function index(Request $request)
    {   

        // $info = Model::where('CRI_ID',34)->find();

        // $list = Model::where('CRI_SUPERIOR_CODE',$info['CRI_CODE'])->select();

        // for ($i=0; $i < count($list); $i++) { 
            
        //      $weishu = strlen(floor($list[$i]['CRI_ID']));
        //      $newid = 0;

        //      if ($weishu == 1) {
                 
        //          $newid = '000'.$list[$i]['CRI_ID'];

        //      }else if ($weishu == 2) {

        //          $newid = '00'.$list[$i]['CRI_ID'];

        //      }else if ($weishu == 3) {

        //          $newid = '0'.$list[$i]['CRI_ID'];

        //      }else if ($weishu == 4) {

        //          $newid = $list[$i]['CRI_ID'];
        //      }

        //      Model::where('CRI_ID',$list[$i]['CRI_ID'])->update(['id_path'=>$info['id_path'].$newid]);

        //      $info1 = Model::where('CRI_ID',$list[$i]['CRI_ID'])->find();

        //      $list1 = Model::where('CRI_SUPERIOR_CODE',$info1['CRI_CODE'])->select();

        //      for ($k=0; $k < count($list1); $k++) { 


        //          $weishu1 = strlen(floor($list1[$k]['CRI_ID']));
        //          $newid1 = 0;

        //          if ($weishu1 == 1) {
                     
        //              $newid1 = '000'.$list1[$k]['CRI_ID'];

        //          }else if ($weishu1 == 2) {

        //              $newid1 = '00'.$list1[$k]['CRI_ID'];

        //          }else if ($weishu1 == 3) {

        //              $newid1 = '0'.$list1[$k]['CRI_ID'];

        //          }else if ($weishu1 == 4) {

        //              $newid1 = $list1[$k]['CRI_ID'];
        //          }

                 
        //          Model::where('CRI_ID',$list1[$k]['CRI_ID'])->update(['id_path'=>$info1['id_path'].$newid1]);
        //      }
        // }
        


        // $list = Model::where('CRI_DATA_STATE',0)->select();
        // for ($i=0; $i < count($list); $i++) { 
                
        //         Model::where('CRI_ID',$list[$i]['CRI_ID'])->update(['CRI_DATA_STATE'=>1]);
        // }
        return $this->fetch();

    }



    //城市列表

    public function gList(Request $request)

    {

        //获取每页显示的条数

        $limit = $request->param('limit');

        //获取当前页数

        $page = $request->param('page');

        //halt($page);

       
        $name = $request->param('name');


        $goodList = Model::with('upinfo')->where('CRI_NAME', 'like', '%' . $name . '%')->order('id_path')->paginate($limit, false, ['page' => $page])->toArray();

        //halt($goodList);
        return json(['code' => 0, 'msg' => '', 'count' => $goodList['total'], 'data' => $goodList['data']]);

    }




    public function editSxj()

    {
        $id    = $this->request->param('id');
        $is_xj = $this->request->param('is_xj');

        $info = Model::where('CRI_ID', $id)->find();

        $arr = Model::where('id_path','like',$info['id_path'] . '%')->select();

        for ($i=0; $i < count($arr); $i++) { 
                
                Model::where('CRI_ID', $arr[$i]['CRI_ID'])->update(['CRI_DATA_STATE' => $is_xj]);
        }


        if ($info['CRI_LEVEL'] > 1 && $is_xj == 1) {
                
                $cityinfo1 = Model::where('CRI_CODE', $info['CRI_SUPERIOR_CODE'])->find();

                if ($cityinfo1) {

                    Model::where('CRI_ID', $cityinfo1['CRI_ID'])->update(['CRI_DATA_STATE' => $is_xj]);

                    if ($cityinfo1['CRI_LEVEL'] > 1) {

                        Model::where('CRI_CODE', $cityinfo1['CRI_SUPERIOR_CODE'])->update(['CRI_DATA_STATE' => $is_xj]);
                    }
                }
        }

        // return json(['code'=>0, 'message'=>'品牌、车型名称已存在']);
        return 1;

       
    }



    public function editBase()

    {
        $id    = $this->request->param('id');
        $base = $this->request->param('base');

        $info = Model::where('CRI_ID', $id)->find();

        $arr = Model::where('id_path','like',$info['id_path'] . '%')->select();

        for ($i=0; $i < count($arr); $i++) { 
                
                Model::where('CRI_ID', $arr[$i]['CRI_ID'])->update(['base' => $base]);
        }

        //return json(['code'=>0, 'message'=>'品牌、车型名称已存在']);
        return 1;

       
    }



}

